package org.eztouch.netty.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.eztouch.netty.Constants;
import org.eztouch.netty.base.AskMsg;
import org.eztouch.netty.base.AskParams;
import org.eztouch.netty.base.LoginMsg;


/**
 * netty 客户端启动类
 *
 */
public class NettyClientBootstrap {
	private int port;  
    private String host;  
    private SocketChannel socketChannel;  
    private static final EventExecutorGroup group = new DefaultEventExecutorGroup(20);  
    public NettyClientBootstrap(int port, String host) throws InterruptedException {  
        this.port = port;  
        this.host = host;  
        start();  
    }  
    private void start() throws InterruptedException {  
        EventLoopGroup eventLoopGroup=new NioEventLoopGroup();  
        Bootstrap bootstrap=new Bootstrap();  
        bootstrap.channel(NioSocketChannel.class);  
        bootstrap.option(ChannelOption.SO_KEEPALIVE,true);  
        bootstrap.group(eventLoopGroup);  
        bootstrap.remoteAddress(host,port);  
        bootstrap.handler(new ChannelInitializer<SocketChannel>() {  
            @Override  
            protected void initChannel(SocketChannel socketChannel) throws Exception {  
                socketChannel.pipeline().addLast(new IdleStateHandler(20,10,0));  
                socketChannel.pipeline().addLast(new ObjectEncoder());  
                socketChannel.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));  
                socketChannel.pipeline().addLast(new NettyClientHandler());  
            }  
        });  
        ChannelFuture future =bootstrap.connect(host,port).sync();  
        if (future.isSuccess()) {  
            socketChannel = (SocketChannel)future.channel();  
            System.out.println("--------->【客户端】连接成功！！<---------");  
        }  
    }  
    public static void main(String[]args) throws InterruptedException, IOException {  
        Constants.setClientId("001");  
        NettyClientBootstrap bootstrap=new NettyClientBootstrap(9999,"localhost");  
  
        LoginMsg loginMsg=new LoginMsg();  
        loginMsg.setUserName("xiaojl");  
        loginMsg.setPassword("xiaojl");  
        bootstrap.socketChannel.writeAndFlush(loginMsg);  
          
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));  
        while (true) {  
            String msg = console.readLine();  
            if (msg == null) {  
                break;  
            } else if ("bye".equals(msg.toLowerCase())) {  
                break;  
            } else if ("ping".equals(msg.toLowerCase())) {  
            } else {  
              AskMsg askMsg=new AskMsg();  
              AskParams askParams=new AskParams();  
              askParams.setAuth("authToken");  
              askParams.setContent(msg);  
              askMsg.setParams(askParams);  
              bootstrap.socketChannel.writeAndFlush(askMsg);  
            }  
        }  
    } 
}
